package com.example.yls.demoa;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button btnText;
    private Button btn_Img;
    private Button btnVideo;
    private ListView lv_list;
    private Intent intent;
    private MyAdapter adapter;
    private Notesdb notesdb;
    private SQLiteDatabase dbwriter;
    private Cursor cursor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnText = findViewById(R.id.text);
        btn_Img = findViewById(R.id.img);
        btnVideo = findViewById(R.id.video);
        btnText.setOnClickListener((View.OnClickListener) this);
        btn_Img.setOnClickListener((View.OnClickListener) this);
        btnVideo.setOnClickListener((View.OnClickListener) this);
        notesdb = new Notesdb(this);
        dbwriter = notesdb.getWritableDatabase();

        lv_list = findViewById(R.id.list);
        lv_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                cursor.moveToPosition(position);
                Intent intent = new Intent(MainActivity.this, SelectActivity.class);
                intent.putExtra(notesdb.id,
                        cursor.getInt(cursor.getColumnIndex(notesdb.id)));
                intent.putExtra(Notesdb.content, cursor.getString(cursor
                        .getColumnIndex(Notesdb.content)));
                intent.putExtra(Notesdb.time,
                        cursor.getString(cursor.getColumnIndex(Notesdb.time)));
                intent.putExtra(Notesdb.path,
                        cursor.getString(cursor.getColumnIndex(Notesdb.path)));
                intent.putExtra(Notesdb.video,
                        cursor.getString(cursor.getColumnIndex(Notesdb.video)));
                startActivity(intent);
            }
        });
    }
    @Override
    public void onClick(View v) {
        intent = new Intent(this, AddActivity.class);
        switch (v.getId()) {
            case R.id.text:
                intent.putExtra("flag", "1");
                startActivity(intent);
                break;

            case R.id.img:
                intent.putExtra("flag", "2");
                startActivity(intent);
                break;

            case R.id.video:
                intent.putExtra("flag", "3");
                startActivity(intent);
                break;
        }
    }

    public void selectDB() {
        cursor = dbwriter.query(Notesdb.table_name, null, null, null, null,
                null, null);
        adapter = new MyAdapter(this, cursor);
        lv_list.setAdapter(adapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        selectDB();
    }

}
